package com.example.admin.controller.question;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.question.entity.Question;
import com.example.question.entity.Result;
import com.example.question.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题目控制器
 * @author 颜殿
 */
@RestController
@RequestMapping("/api/question")
@CrossOrigin
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 基础分页查询
     * @param pageNum 页码
     * @param pageSize 条数
     * @return 返回数据
     */
    @PostMapping("/page")
    public Result<Page<Question>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize) {
        Page<Question> page = new Page<>(pageNum, pageSize);
        return Result.success(questionService.page(page));
    }

    /**
     * 添加题目
     * @param question 题目实体
     * @return 操作结果
     */
    @PostMapping
    public Result<Boolean> addQuestion(@RequestBody Question question) {
        return Result.success(questionService.addQuestion(question));
    }

    /**
     * 删除题目
     * @param id 要删除的题目ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteQuestion(@PathVariable Integer id) {
        return Result.success(questionService.deleteQuestion(id));
    }

    /**
     * 批量删除题目
     * @param ids 要删除的题目ID列表
     * @return 操作结果
     */
    @DeleteMapping("/batch")
    public Result<Boolean> batchDeleteQuestions(@RequestBody List<Integer> ids) {
        return Result.success(questionService.batchDeleteQuestions(ids));
    }

    /**
     * 修改题目
     * @param question 题目实体
     * @return 操作结果
     */
    @PutMapping
    public Result<Boolean> updateQuestion(@RequestBody Question question) {
        return Result.success(questionService.updateQuestion(question));
    }

    /**
     * 根据分类分页查询题目
     * @param pageNum 页码
     * @param pageSize 条数
     * @param name 分类名称
     * @return 返回数据
     */
    @PostMapping("/page/category")
    public Result<Page<Question>> pageByCategory(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam String name) {
        return Result.success(questionService.pageByCategory(pageNum, pageSize, name));
    }

    /**
     * 根据难度分页查询题目
     * @param pageNum 页码
     * @param pageSize 条数
     * @param difficulty 难度
     * @return 返回数据
     */
    @PostMapping("/page/difficulty")
    public Result<Page<Question>> pageByDifficulty(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam String difficulty) {
        return Result.success(questionService.pageByDifficulty(pageNum, pageSize, difficulty));
    }

    /**
     * 根据状态分页查询题目
     * @param status 状态
     * @param pageNum 页码
     * @param pageSize 条数
     * @return 返回数据
     */
    @PostMapping("/page/status/{status}")
    public Result<Page<Question>> pageByStatus(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @PathVariable Integer status) {
        return Result.success(questionService.pageByStatus(pageNum, pageSize, status));
    }

    /**
     * 根据描述分页查询题目
     * @param pageNum 页码
     * @param pageSize 条数
     * @param description 描述关键词
     * @return 返回数据
     */
    @PostMapping("/page/description")
    public Result<Page<Question>> pageByDescription(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam String description) {
        return Result.success(questionService.pageByDescription(pageNum, pageSize, description));
    }

    /**
     * 根据题目集ID查询题目
     * @param setId 题目集ID
     * @return 返回数据
     */
    @GetMapping("/set/{setId}")
    public Result<List<Question>> getQuestionsBySetId(@PathVariable Integer setId) {
        return Result.success(questionService.getQuestionsBySetId(setId));
    }

    /**
     * 根据题目集ID获取题目数量
     * @param problemsetId 题目集ID
     * @return 题目数量
     */
    @GetMapping("/count/byProblemsetId/{problemsetId}")
    public Result<Long> countByProblemsetId(@PathVariable Integer problemsetId) {
        return Result.success(questionService.countByProblemsetId(problemsetId));
    }

    /**
     * 获取单个问题详情
     * @param id 问题ID
     * @return 问题详情
     */
    @GetMapping("/{id}")
    public Result<Question> getQuestionById(@PathVariable Integer id) {
        return Result.success(questionService.getById(id));
    }
} 